﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HTES.TAS.Plugin;
using System.Threading;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using RS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using HTES.TAS.Engine.Extend;
using System.Drawing;
using ESRI.ArcGIS.Display;

namespace RS
{
    /*!
     * \class RSAgricutureDroughtWaterloggingPlugin 插件类
     *
     * \brief 
     *
     */
    public class RSAgricutureDroughtWaterloggingPlugin : PluginBase, IPlugin
    {
        private RSAgricutureDroughtWaterloggingUI mTHPluginUserControl = null;
        private CancellationTokenSource tokenSource = new CancellationTokenSource();
        private CancellationToken token = CancellationToken.None;

        /*!
         * \brief 构造函数，实现类的初始化
         *
         * \param 无 
         *
         * \return 无
         */
        public RSAgricutureDroughtWaterloggingPlugin()
        {
            //插件描述
            this.Description = "AgricutureDroughtWaterlogging Plugin by zlx";

            //插件名称
            this.Name = "AgricutureDroughtWaterloggingPlugin";

            //插件版本，与设置-应用程序-程序集 中的设置相同
            //通过设置对插件版本进行控制
            this.Version = "1.0.0.0";

            //创建参数对话框
            mTHPluginUserControl = new RSAgricutureDroughtWaterloggingUI { Plugin = this };
        }

        public override UserControl GetArgumentPane()
        {
            return mTHPluginUserControl;
        }

        public override void Start()
        {
            //设置状态
            this.State = PluginState.Starting;

            //日志输出
            this.Host.Log(this, LogLevel.Info, "Agricuture", "Start");
            //进程为0
            ProgressPercent = 0;
            //参数路径获取
            String inputImg1 = mTHPluginUserControl.inputImg1Path();
            String inputImg2 = mTHPluginUserControl.inputImg2Path();
            String OutputImg = mTHPluginUserControl.outputImgPath();
            int nFlag = mTHPluginUserControl.flag();

            //参数合法性判断
            if (null == inputImg1 || !File.Exists(inputImg1))
            {
                this.Host.Log(this, LogLevel.Info, "Agricuture", "Input Image1 Path is invalid");
                this.State = PluginState.Finished;
            }
            if (null == inputImg2 || !File.Exists(inputImg2))
            {
                this.Host.Log(this, LogLevel.Fatal, "Agricuture", "Input Image2 Path is invalid");
                this.State = PluginState.Finished;
            }
            if (null == OutputImg)
            {
                this.Host.Log(this, LogLevel.Fatal, "Agricuture", "Output Image Path is NULL");
                this.State = PluginState.Finished;
            }
            FileInfo fileInfo = new FileInfo(OutputImg);
            if (fileInfo.Attributes == FileAttributes.Directory)
            {
                this.Host.Log(this, LogLevel.Fatal, "Agricuture", "Output Image Path is Directory");
                this.State = PluginState.Finished;
            }
            //处理算法
            var rs = new RSAgricutureDroughtWaterloggingWrapper();
            if (null != rs)
            {
                //设置参数
                rs.setFlag(nFlag);
                if (!rs.setInputImg(inputImg1))
                {
                    this.Host.Log(this, LogLevel.Info, "Agricuture", rs.getMsg());
                    return;
                }
                if (!rs.setNewInputImg(inputImg2))
                {
                    this.Host.Log(this, LogLevel.Info, "Agricuture", rs.getMsg());
                    return;
                }
                rs.setOutputImgFormat(OutputImg);
                rs.setFunctionCallBack(showProgressText);

                this.Host.Log(this, LogLevel.Info, "Agricuture", "Dispose……");

                //处理算法
                if (!rs.disPose())
                {
                    this.Host.Log(this, LogLevel.Info, "Agricuture", rs.getMsg());
                    return;
                }
            }
// 
//             var mapControl = this.Host.MapControl;
//             var pGraContainer = mapControl.Map as IGraphicsContainer;
// 
//             IActiveView IActiveView = mapControl.ActiveView;
//             var symbol = ExtendSymbol.CreateSimpleFillSymbol(Color.Red, 5, esriSimpleFillStyle.esriSFSSolid);
//             //画直线
//             IGeometry geometry = CreateGeometry();
//             ExtendElement.AddCreateElement(geometry, IActiveView, symbol, "ID1");


            for (int i = 0; i < 100; i++)
            {
                token.WaitHandle.WaitOne(1);
                ProgressPercent = i;
                this.Host.Log(this, LogLevel.Info, "", "contu：" + i.ToString());
            }
//             token = tokenSource.Token;
//             ProgressPercent = 0;
//             var task = new Task(() =>
//             {
//                 bool cancelled = false;
//                 for (int i = 0; i <= 100; i++)
//                 {
//                     if (i == 100)
//                     {
//                         this.State = PluginState.Finished;
// 
//                         var mapControl = this.Host.MapControl;
//                         var pGraContainer = mapControl.Map as IGraphicsContainer;
// 
//                         IActiveView IActiveView = mapControl.ActiveView;
//                         var symbol = ExtendSymbol.CreateSimpleFillSymbol(Color.Red, 5, esriSimpleFillStyle.esriSFSSolid);
//                         //画直线
//                         IGeometry geometry = CreateGeometry();
//                         ExtendElement.AddCreateElement(geometry, IActiveView, symbol, "ID1");
//                     }
//                     ProgressPercent = i;
//                     cancelled = token.WaitHandle.WaitOne(1);
//                     if (cancelled)
//                     {
//                         break;
//                     }
//                     else
//                     {
//                         this.Host.Log(this, LogLevel.Info, "", "contu：" + i.ToString());
//                     }
//                 }
//                 if (cancelled)
//                 {
//                 }
//                 else
//                 {
//                     //ResultFile = argumentOutputFile;//给出输出文件路径
//                     this.State = PluginState.Finished;
//                 }
//             }, token);
//             task.Start();

            //进程为100
            //ProgressPercent = 100;

//             token = tokenSource.Token;
//             ProgressPercent = 0;
//             var task = new Task(() =>
//             {
//                 var rs = new RSAgricutureDroughtWaterloggingWrapper();
//                 if (null != rs)
//                 {
//                     //设置参数
//                     rs.setFlag(nFlag);
//                     if (!rs.setInputImg(inputImg1))
//                     {
//                         this.Host.Log(this, LogLevel.Info, "Agricuture", rs.getMsg());
//                         return;
//                     }
//                     if (!rs.setNewInputImg(inputImg2))
//                     {
//                         this.Host.Log(this, LogLevel.Info, "Agricuture", rs.getMsg());
//                         return;
//                     }
//                     rs.setOutputImgFormat(OutputImg);
//                     rs.setFunctionCallBack(showProgressText);
// 
//                     this.Host.Log(this, LogLevel.Info, "Agricuture", "Dispose……");
// 
//                     //处理算法
//                     if (!rs.disPose())
//                     {
//                         this.Host.Log(this, LogLevel.Info, "Agricuture", rs.getMsg());
//                         return;
//                     }
//                 }
// 
//                 var mapControl = this.Host.MapControl;
//                 var pGraContainer = mapControl.Map as IGraphicsContainer;
// 
//                 IActiveView IActiveView = mapControl.ActiveView;
//                 var symbol = ExtendSymbol.CreateSimpleFillSymbol(Color.Red, 5, esriSimpleFillStyle.esriSFSSolid);
//                 //画直线
//                 IGeometry geometry = CreateGeometry();
//                 ExtendElement.AddCreateElement(geometry, IActiveView, symbol, "ID1");
// 
// 
//              }, token
//             );
//             task.Start();

        }

        public override void Exit()
        {
            this.State = PluginState.Exiting;
        }

        public override bool VerifyArguments()
        {
            return (null != mTHPluginUserControl) && (mTHPluginUserControl.VerifyArguments());
        }

        private void showProgressText(int nPos, String text)
        {
            ProgressPercent = nPos;
            this.Host.Log(this, LogLevel.Info, "", text);
        }

        private IGeometry CreateGeometry()
        {
            IPoint pPoint1 = new PointClass { X = -900, Y = 0 };
            IPoint pPoint2 = new PointClass { X = 0, Y = 200 };

            IPolygon polygon = new PolygonClass();
            polygon.FromPoint = pPoint1;
            polygon.ToPoint = pPoint2;

            return polygon;
        }

        private IGeometry CreatePolygonGeometry()
        {
            IPoint pPoint1 = new PointClass { X = 100, Y = 100 };
            IPoint pPoint2 = new PointClass { X = 200, Y = 200 };

            IPolygon polygon = new PolygonClass();
            polygon.FromPoint = pPoint1;
            polygon.ToPoint = pPoint2;

            return polygon;
        }
    }
}
